package org.nerve.webmagic;

import org.nerve.webmagic.pool.CounterPool;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.QueueScheduler;

/**
 * Created by live on 2016/3/30.
 */
public class DeepLimitScheduler extends QueueScheduler {
    public static final String LEVEL = "_level";
    private int maxLevel=2;

    public DeepLimitScheduler(){}
    public DeepLimitScheduler(int level){
        maxLevel=level;
        logger.info("DeepLimitScheduler inited! max deep level is "+maxLevel);
    }

    @Override
    public synchronized void push(Request request, Task task) {
        Object l=request.getExtra(LEVEL);
        if(l==null||((Integer)l)<maxLevel){
            CounterPool.addCrawl();
            super.push(request, task);
        }
    }
}
